package com.example.laboratory.root_ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import com.example.laboratory.R;

import com.example.laboratory.root_ui.AddLaboratory.AddLaboratoryActivity;
import com.example.laboratory.ui.user.Laboratory.LabListActivity;

import java.util.List;

/**
 * @author: Created by lzl on 2017/6/17.
 * @function: 自定义单选列表dialog
 * @description:
 */

public class LabDialog extends Dialog {
    private Button  mCloseImg;//关闭按钮
    private Button mSureBtn;//确定选择按钮
    private ListView mListView;//列表
    private Context mContext;
    private List<String> mList;
    private TextView dialog_title;
    private int posi;
    /**
     * 记录当前选中的值
     */
    private String currentSelectItemName;
    /**
     * 是否被点击了 如果被点击了 则此时可以点击确定按钮
     */
    private boolean isBeClick=false;
    public LabDialog(Context context, List<String> list) {
        super(context, R.style.dialog);
        this.mContext=context;
        this.mList=list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xjlab_selected);
        //点击外部区域不可取消
        setCanceledOnTouchOutside(false);

       // dialogConfirmPay.setOnClickListener(onClickListener);
        initView();
        initListViewData();
        initEvent();
    }

    /**
     * 加载列表数据
     */
    private void initListViewData() {
        mListView.setAdapter(new ArrayAdapter<String>(mContext,R.layout.support_simple_spinner_dropdown_item,mList));
    }

    private void initView() {
        mCloseImg=  (Button) findViewById(R.id.single_select_cancel);
        mSureBtn= (Button) findViewById(R.id.single_select_sure_btn);
        mListView= (ListView) findViewById(R.id.singselect_list);
        dialog_title=findViewById(R.id.dialog_title);
        dialog_title.setText("实验室");
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = AbsListView.LayoutParams.MATCH_PARENT;
        lp.y = 0;//设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp);

    }

    private void initEvent() {
        mCloseImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mSureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            if (currentSelectItemName!=null&&!currentSelectItemName.equals("")){
                if(posi==0){
                    Intent intent = new Intent(mContext, LabListActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable(Const.BUNDLE_KEY.OBJ, mList.get(posi));
//                intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }else if(posi==1){
                    Intent intent = new Intent(mContext, AddLaboratoryActivity.class);
                    mContext.startActivity(intent);
                }


                dismiss();
                }else {
                Toast.makeText(mContext,"您当前未选择任何项",Toast.LENGTH_SHORT).show();
            }

            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if(mList!=null){
                    currentSelectItemName=mList.get(position);
                    posi=position;
                }
            }
        });
    }
}